// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Button from './button';
import * as common from './common'
import resource from './resource';
const {ccclass, property} = cc._decorator;
const client = common.Client.get()
const log = console.log

import {roomManager, userGameInfo} from './room'

import { pukeManeger } from './wxcloud';
import { handOut } from './json';
import userInfo from './userInfo';
import { cardsShower, handCardManager } from './cardManager';
import { outCardCheck } from './pukeData';


export class buttonTip{
    anchor:cc.Node
    buttons: Array<Button> = []
    constructor(btnsinfo:Array<object>,position:cc.Vec2=null){
        this.anchor = new cc.Node()
        if(!position) position = cc.v2(0, -120)
        this.anchor.setPosition(position)
        this.anchor.active = false
        cc.find("Canvas").addChild(this.anchor)
        btnsinfo.map(btn=>{
            var button:Button = cc.instantiate(cc.find("Canvas").getComponent(resource).button).getComponent(Button)
            button.Init(btn)
            this.anchor.addChild(button.node)
            this.buttons.push(button)
        })
        this.centerify()
    }
    centerify(){
        var deltaX = 120
        var num = this.buttons.length
        for(var i=0;i<Math.floor(num/2);i++){
            this.buttons[i].node.x = -(deltaX*(Math.floor(num/2)-i))
            this.buttons[this.buttons.length - 1 - i].node.x = (deltaX*(Math.floor(num/2)-i))
        }
    }
    show(active=true){
        this.anchor.active = active
    }
    destroy(){
        this.buttons.map(btn=>btn.destroy())
        this.anchor.destroy()
    }
}

@ccclass
export default class interactive extends cc.Component{
    prepareButton: buttonTip
    grapButton: buttonTip
    outButton: buttonTip
    clickCnt=0
    onLoad(){
        roomManager.get()
        common.Client.get()
        common.Client.get().receiveHandler = roomManager.get().receive
        common.Client.get().connect("joinPlayer")
    }
    start(){
        // common.setUid('111111')
        // // var handCard = new handCardManager()
        // // handCard.display(handOut.roomInfo.players['111111'].handCards)
        
        // cc.find("Canvas/test").on(cc.Node.EventType.TOUCH_END,touch=>{
        //     // handCard.playOut()
        //     handOut.event = 'joinPlayer'
        //     roomManager.get().receive(handOut)
        //     roomManager.get().usersInfo['111111'].Avatar.showHat()
        //     // handOut.event = 'handOutCards'
        //     // roomManager.get().receive(handOut) 
        //     // handOut.roomInfo.timeto = common.uid
        //     // handOut.event = 'grabCardRes'
        //     // roomManager.get().receive(handOut) 


        //     if(this.clickCnt==0){
        //         // user.showHat()
        //     }
        //     else if(this.clickCnt==1){
        //     }
        //     this.clickCnt++
        // })

        // setTimeout(() => {
        //     client.doTask("preparePlayer") //test
        // }, 500);

        common.Client.get().gameOverHandler = (data)=>{
            cc.find("Canvas/gameOver").active = true
            cc.find("Canvas/gameOver").zIndex = 99
            cc.find("Canvas/gameOver/gameResult").getComponent(cc.Label).string = data.gameResult[common.uid] ? "win" : "lose"
            cc.find("Canvas/gameOver/back").on(cc.Node.EventType.TOUCH_END, touch => {
                cc.director.loadScene("login")
            })
        }

        this.prepareButton = new buttonTip([
            {
                color: 'blue', text: '准备', handler:()=>{
                    this.prepareButton.show(false)
                    client.doTask("preparePlayer")
                }
            },
        ])

        this.prepareButton.show(true)

        var that = this
        common.Client.get().handOutCardsHandler = (data)=>{
            if(data.roomInfo.timeto == common.uid){
                that.grapButton.show(true)
                // client.doTask('grabCard',{grabRes:1}) //test
            }
                
        }
        this.grapButton = new buttonTip([
            {
                color: 'blue', text: '不抢', handler:()=>{
                    this.grapButton.show(false)
                    client.doTask('grabCard',{grabRes:0})
                }
            },
            {
                color: 'yellow', text: '抢地主', handler:()=>{
                    this.grapButton.show(false)
                    client.doTask('grabCard',{grabRes:1})
                }
            },
        ])
        common.Client.get().playOutHandler = (data)=>{
            if(data.roomInfo.timeto == common.uid)
                that.outButton.show(true);
        }
        this.outButton = new buttonTip([
            {
                color: 'blue', text: '不出', handler:()=>{
                    this.outButton.show(false)
                    client.doTask('playOut',{ outStyle: null ,outCards:[] })
                }
            },
            {
                color: 'yellow', text: '出牌', handler:()=>{
                    var param = roomManager.get().userPlayOut()
                    if(param){
                        this.outButton.show(false)
                        client.doTask('playOut',param)
                    }
                }
            },
        ])
    }
}
